package com.ds.dsm.view.nav.service.gallery;

import com.ds.common.JDSException;
import com.ds.config.ResultModel;
import com.ds.config.TreeListResultModel;
import com.ds.dsm.view.config.gallery.GalleryInfoGroup;
import com.ds.enums.CustomBean;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.bean.ApiClassConfig;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.bean.nav.gallery.NavGalleryViewBean;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.layout.CustomLayoutItemBean;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.NavGroupViewAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.esd.dsm.view.field.FieldFormConfig;
import com.ds.esd.dsm.view.field.FieldModuleConfig;
import com.ds.esd.tool.ui.enums.Dock;
import com.ds.esd.util.TreePageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/dsm/view/config/navgallery/")
public class ViewNavGalleryConfigService {


    @RequestMapping(method = RequestMethod.POST, value = "ViewGalleryConfig")
    @ModuleAnnotation(dynLoad = true, caption = "获取字段列", imageClass = "bpmfont bpmgongzuoliuxitongpeizhi", index = 0)
    @APIEventAnnotation(bindMenu = CustomMenuItem.loadChild)
    @ResponseBody
    public TreeListResultModel<List<GalleryNavTree>> getViewGroupConfig(String domainId, String sourceClassName, String methodName, String viewInstId) {
        TreeListResultModel<List<GalleryNavTree>> result = new TreeListResultModel<>();

        try {
            List<CustomBean> customBeans = new ArrayList<>();
            ViewEntityConfig classConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            ApiClassConfig customESDClassAPIBean = classConfig.getSourceConfig();
            MethodConfig methodAPIBean = customESDClassAPIBean.getMethodByName(methodName);
            NavGalleryViewBean navTabsViewBean = (NavGalleryViewBean) methodAPIBean.getView();
            CustomLayoutItemBean layoutItemBean = navTabsViewBean.getLayoutItemBean();
            customBeans.add(layoutItemBean);
            List<String> fieldNames = navTabsViewBean.getDisplayFieldNames();
            for (String fieldName : fieldNames) {
                FieldFormConfig fieldFormConfig = (FieldFormConfig) navTabsViewBean.getFieldConfigMap().get(fieldName);
                if (fieldFormConfig != null) {
                    customBeans.add(fieldFormConfig);
                }
            }
            List<String> itemNames = navTabsViewBean.getModuleFields();
            for (String fieldName : itemNames) {
                FieldModuleConfig fieldFormConfig = navTabsViewBean.getFieldModuleConfig(fieldName);
                if (fieldFormConfig != null) {
                    customBeans.add(fieldFormConfig);
                }
            }
            result = TreePageUtil.getTreeList(customBeans, GalleryNavTree.class);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return result;
    }


    @RequestMapping(method = RequestMethod.POST, value = "GroupInfoGroup")
    @NavGroupViewAnnotation(reSetUrl = "clear", saveUrl = "updateTabsBase", autoSave = true)
    @ModuleAnnotation(dock = Dock.fill, caption = "Group配置", index = 0, imageClass = "spafont spa-icon-project")
    @APIEventAnnotation(bindMenu = CustomMenuItem.treeNodeEditor)
    @ResponseBody
    public ResultModel<GalleryInfoGroup> getGroupInfoGroup(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ResultModel<GalleryInfoGroup> result = new ResultModel<GalleryInfoGroup>();
        return result;
    }


}
